<template>
  <div class="app-container">
    <!-- 查询框 -->
    <div class="filter-container">
      <el-input
        v-model="fieldQuery.realName"
        style="width: 200px;"
        placeholder="菜单名称查询"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.realName" style="width: 140;" class="filter-item" placeholder="创建时间">
        <el-option v-for="item in orderOptions" :key="item.key" :label="item.label" :value="item.value" />
      </el-select>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table :data="list" fit highlight-current-row style="width: 100%;">

      <el-table-column class="filter-item" type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="菜单名称">
              <span>{{ row.menuName }}</span>
            </el-form-item>
            <el-form-item label="创建人">
              <span>{{ row.createByUser }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ row.createDate }}</span>
            </el-form-item>
            <el-form-item label="修改人">
              <span>{{ row.updateByUser }}</span>
            </el-form-item>
            <el-form-item label="修改时间">
              <span>{{ row.updateDate }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="menuName" label="菜单名称" />
      <el-table-column prop="createByUser" label="创建人" />
      <el-table-column prop="createDate" label="创建时间" />
      <el-table-column sortable="custom" prop="updateByUser" label="修改人" />

      <el-table-column label="操作" fit>
        <template slot-scope="{row,$index}">
          <el-button type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getListByPage" />
    <!-- 弹窗组件 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="生日" prop="birth">
          <el-date-picker v-model="temp.birth" type="datetime" placeholder="Please pick a date" />
        </el-form-item>
        <el-form-item label="菜单名称" prop="realName">
          <el-input v-model="temp.realName" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="temp.nickName" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="temp.age" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">确认</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
// 导入user的api
import {
  listByPage
} from '@/api/menu'
  // 导入组件  记得声明组件
import Pagination from '@/components/Pagination'

export default {
  components: { // 在这里声明组件
    Pagination
  },
  data() {
    return {
      list: [], // 列表数据
      total: 0,
      orderOptions: [{
        label: '年龄 升序',
        key: '+age',
        value: 'age asc'
      },
      {
        label: '年龄 降序',
        key: '-age',
        value: 'age desc'
      }
      ],

      listQuery: { // 分页 排序  查询参数
        page: 1,
        limit: 6
      },
      fieldQuery: { // 按字段查询
        realName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogPvVisible: false,
      pvData: [],
      temp: {
        id: undefined,
        realName: '',
        nickName: '',
        age: undefined,
        birth: undefined
      },
      rules: {}

    }
  },
  created() {
    this.getListByPage()
  },
  methods: {
    getListByPage() {
      const listQuery = this.listQuery
      const fieldQuery = this.fieldQuery
      const queryData = {
        listQuery,
        fieldQuery
      }
      listByPage(queryData).then(res => {
        // console.log(res)
        this.list = res.content.list
        this.total = res.content.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getListByPage()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.birth = new Date(this.temp.birth)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row, index) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    createData() {
      this.dialogFormVisible = false
      this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
      })
    },
    updateData() {
      this.dialogFormVisible = false
      this.$notify({
        title: '成功',
        message: '编辑成功',
        type: 'success',
        duration: 2000
      })
    },
    resetTemp() {

    }
  }

}
</script>

<style>
  .demo-table-expand {
      font-size: 0;
    }
    .demo-table-expand label {
      width: 90px;
      color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 50%;
    }
</style>
